from django.http                    import HttpResponse, Http404
from django.shortcuts               import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.utils.decorators        import method_decorator
from django.views.generic           import TemplateView, ListView
from django.views.generic.edit      import CreateView

from inmobiliarias.models import *
from inmobiliarias.forms import *


#vistas genericas
class InmobiliariasView(TemplateView):
    def get_context_data(self, **kwargs):
        context = dict()
        context['current'] = getattr(self, 'current', None)
        return context
        

class InmobiliariasListView(ListView):
	def get_context_data(self, **kwargs):
		context = dict()
		context['current'] = getattr(self, 'current', None)
		return context

class GenericInicioView(InmobiliariasView):
    current = 'INICIO'

class GenericListView(InmobiliariasListView):
    current = 'List'


##vistas concretas
class HomeView(GenericInicioView):
    template_name = 'website/home.html'

    def get_context_data(self, **kwargs):
        context = super(GenericInicioView, self).get_context_data(**kwargs)
        context['formularioBusqueda'] = PropiedadSearchForm()#ver si ya la cargo no valido
        return context

class ResultadoBusquedaView(ListView):
    context_object_name = "propiedades"
    template_name = 'website/resultado.html'
    paginate_by = 20
   
    def get_queryset(self):
		form = PropiedadSearchForm(self.request.GET)
		if form.data['tipo'] == 'Casa':
			TipoPropiedad = Casa
		if form.data['tipo'] == 'Departamento':
			TipoPropiedad = Departamento
		if form.data['tipo'] == 'Campo':
			TipoPropiedad = Campo			
		model = TipoPropiedad.objects.filter(
				ciudad=form.data['ciudad'], provincia=form.data['provincia'], 
				precio__gte = form.data['precio_desde'], precio__lte = form.data['precio_hasta'])

		return model

    def get_context_data(self, **kwargs):
		context = super(ResultadoBusquedaView, self).get_context_data(**kwargs)
		context['formularioBusqueda'] = PropiedadSearchForm()
		return context

class CrearDpto(CreateView):
    form_class = DepartamentoForm
    model = Departamento

#class Busqueda(CreateView):
#    form_class = PropiedadForm
#    model = Propiedad
#    def form_valid(self, form):
#        #form.instance.publicante = self.request.user esto para publicar casa por inmobiliaria
#        return super(Busqueda, self).form_valid(form)

